package srv

import (
	"background/model/back/repo"
	"background/utilx/cachex"
	"sync"
)

var lock = &sync.Mutex{} //互斥锁

var singleCloudOrder *CloudOrder

func NewCloudOrder(db repo.Query, bucket cachex.RedisBucket) CloudOrder {
	if singleCloudOrder == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudOrder == nil {
			return newCloudOrder(db, bucket)
		}
	}
	return *singleCloudOrder
}

var singleCloudUser *CloudUser

func NewCloudUser(db repo.Query, bucket cachex.RedisBucket) CloudUser {
	if singleCloudUser == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudUser == nil {
			return newCloudUser(db, bucket)
		}
	}
	return *singleCloudUser
}

var singleCloudPlan *CloudPlan

func NewCloudPlan(db repo.Query, bucket cachex.RedisBucket) CloudPlan {
	if singleCloudPlan == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudPlan == nil {
			return newCombo(db, bucket)
		}
	}
	return *singleCloudPlan
}

var singleCloudLogTraffic *CloudLogTraffic

func NewCloudLogTraffic(db repo.Query, bucket cachex.RedisBucket) CloudLogTraffic {
	if singleCloudLogTraffic == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudLogTraffic == nil {
			return newCloudLogTraffic(db, bucket)
		}
	}
	return *singleCloudLogTraffic
}

var singleCloudNotice *CloudNotice

func NewCloudNotice(db repo.Query, bucket cachex.RedisBucket) CloudNotice {
	if singleCloudNotice == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudNotice == nil {
			return newCloudNotice(db, bucket)
		}
	}
	return *singleCloudNotice
}

var singleCloudPayment *CloudPayment

func NewCloudPayment(db repo.Query, bucket cachex.RedisBucket) CloudPayment {
	if singleCloudPayment == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudPayment == nil {
			return newCloudPayment(db, bucket)
		}
	}
	return *singleCloudPayment
}

var singleCloudServer *CloudServer

func NewCloudServer(db repo.Query, bucket cachex.RedisBucket) CloudServer {
	if singleCloudServer == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudServer == nil {
			return newCloudServer(db, bucket)
		}
	}
	return *singleCloudServer
}

var singleCloudKnowledge *CloudKnowledge

func NewCloudKnowledge(db repo.Query, bucket cachex.RedisBucket) CloudKnowledge {
	if singleCloudKnowledge == nil {
		lock.Lock()
		defer lock.Unlock()
		if singleCloudKnowledge == nil {
			return newCloudKnowledge(db, bucket)
		}
	}
	return *singleCloudKnowledge
}
